﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace TeacherLeftHand.Util
{
    class PositiveIntegerValidationRule : ValidationRule
    {
        #region Overrides of ValidationRule

        /// <summary>
        /// When overridden in a derived class, performs validation checks on a value.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
        /// </returns>
        /// <param name="value">The value from the binding target to check.</param><param name="cultureInfo">The culture to use in this rule.</param>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int number = 0;
            try
            {
                if (((string)value).Length > 0)
                    number = Int32.Parse((String)value);
            }
            catch (FormatException e)
            {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }

            if (number < 0) 
                return new ValidationResult(false,"Please inter a positive number");
            return new ValidationResult(true, null);
        }

        #endregion
    }
}
